package com.cheyun.netsalev3.widget

import android.app.Dialog
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.NameIdParam
import com.cheyun.netsalev3.databinding.PopDialogLayoutBinding
import com.cheyun.netsalev3.utils.AppCongfig
import com.cheyun.netsalev3.utils.NavigationBarUtil
import com.cheyun.netsalev3.utils.DialogUtil
import android.view.WindowManager
import com.tencent.imsdk.utils.IMFunc

class PopDialog : DialogFragment() {
    var listener: PopDialogListener? = null
    private lateinit var binding: PopDialogLayoutBinding
    /**系统仅在对话框中创建布局时调用此操作。*/
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        var dialog = object : Dialog(this!!.activity, theme) {
            override fun show() {
                dialog?.setCanceledOnTouchOutside(true)
                super.show()
                NavigationBarUtil.hideNavigationBar(activity?.window)
            }
        }
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.window.attributes.windowAnimations = R.style.PopAnimation
        return dialog
    }

    override fun onStart() {
        super.onStart()
        // 设置底部弹出显示的DialogFragment窗口属性。
        val window = dialog?.window!!
        var params = window.attributes
        params.gravity = Gravity.BOTTOM
        params.width = WindowManager.LayoutParams.MATCH_PARENT

        params.height = WindowManager.LayoutParams.WRAP_CONTENT // 底部弹出的DialogFragment的高度，如果是MATCH_PARENT则铺满整个窗口
//        params.height = AppCongfig.height / 2
//        params.height = WindowManager.LayoutParams.MATCH_PARENT

        if(!IMFunc.isBrandVivo()){
            DialogUtil.getDialogWindow(this!!.activity, window)
        }
        window.setBackgroundDrawableResource(android.R.color.transparent)
//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

        window.attributes = params
    }

    /**系统调用这个来获得对话片段的布局，不管是否显示为对话框或嵌入片段。*/
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //将布局膨胀为对话框或嵌入片段
        binding = DataBindingUtil.inflate(inflater, R.layout.pop_dialog_layout, container, false)
        listener?.apply {
            binding.viewModel = listener
        }
        NavigationBarUtil.hideNavigationBar(activity?.window)
        initView()
        binding.setLifecycleOwner(this)
        return binding.root
    }

    private fun initView() {
        var adapter = MyAdapter(R.layout.pop_item_layout,binding.viewModel)
        binding.recyclerView.adapter = adapter
//        binding.dialogTitle.text =
        binding.viewModel?.list?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
        binding.viewModel?.dialogType?.observe(this, Observer { type ->
            if(type.equals("edit")) {
                binding.llEdit.visibility = View.VISIBLE
                binding.llSelect.visibility = View.GONE
                val window = dialog?.window!!
                var params = window.attributes
                params.gravity = Gravity.BOTTOM
                params.width = WindowManager.LayoutParams.MATCH_PARENT
                params.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
                window.attributes = params
            }else{
                binding.llSelect.visibility = View.VISIBLE
                binding.llEdit.visibility = View.GONE
                val window = dialog?.window!!
                var params = window.attributes
                params.gravity = Gravity.BOTTOM
                params.width = WindowManager.LayoutParams.MATCH_PARENT
                params.height = AppCongfig.height / 2
                window.attributes = params
            }
        })
        binding.viewModel?.dialogTitle?.observe(this, Observer { title ->
            if(title.isNotEmpty()) binding.dialogTitle.text = title
            else binding.dialogTitle.text = "请选择"
        })
        binding.txtCancel.setOnClickListener {
            this.dismiss()
        }

        binding.btnEditSubmit.setOnClickListener {
            var value = binding.dialogEditText.text.toString()
            binding.dialogEditText.setText("")
            binding.viewModel?.onDialogSubmit(it,value)
        }
    }


    override fun show(manager: FragmentManager, tag: String?) {
//        val dialogWindow = DialogUtil.getDialogWindow(this!!.activity, dialog)
//        val lp = dialogWindow.attributes
//        lp.x = 0
//        lp.y = -20
//        dialogWindow.attributes = lp

        if(!(manager!!.isStateSaved())){
            super.show(manager, tag)
        }
    }

    interface PopDialogListener {
        /**
         * 列表数据
         */
        var list: MutableLiveData<List<NameIdParam>>

        var dialogType: MutableLiveData<String>
        var dialogTitle:MutableLiveData<String>

        /**
         * 列表item点击
         */
        fun onClickItem(view: View, param: NameIdParam)

        fun onDialogSubmit(view:View,param:String)


    }
}